package eft.android.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

// credit for most of this code: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

public class DatabaseHelper extends SQLiteOpenHelper {

	// default path to the database
	private static String DB_PATH = "/data/data/me.croute.calendarexample/databases/";
	
	// database name
	private static String DB_NAME = "exercisedata.db";
	
	private SQLiteDatabase exDatabase;
	
	private final Context exContext;
	
	// NEED AN OPEN METHOD? MAYBE JUST ONE THAT CALLS SUPER?
	
	// List of the labels in the database, makes access easier
	public static final String SAVED_WORKOUTS = "savedworkouts";
	public static final String SW_ID = "_id";
	public static final String SW_TITLE = "swtitle";
	public static final String SW_TYPE = "swtype";
	public static final String SW_DESCRIPTION = "swdescription";
	public static final String SW_DATE = "swdate";
	public static final String SW_REPS = "swreps";
	public static final String SW_TIME = "swtime";
	public static final String SW_WEIGHT = "swweight";
	public static final String SW_NOTES = "swnotes";
	
	public static final String CROSSFIT_WORKOUTS = "crossfitworkouts";
	public static final String CF_ID = "_id";
	public static final String CF_TITLE = "cwtitle";
	public static final String CF_TYPE = "cwtype";
	public static final String CF_DESCRIPTION = "cwdescription";
	public static final String CF_REPS = "cwreps";
	public static final String CF_TIME = "cwtime";
	public static final String CF_WEIGHT = "cwweight";
	
	public static final String DAILY_WEIGHT = "dailyweight";
	public static final String DW_ID = "_id";
	public static final String DW_WEIGHT = "dwweight";
	public static final String DW_DATE = "dwdate";
	
//	private static final int DB_VERSION = 2;
	
//	private String dataPath;
	
	//Constructor, uses SQLiteOpenHelper ctor
	public DatabaseHelper (Context context) //throws IOException
	{
		super (context, DB_NAME, null, 1);
		this.exContext = context;
		boolean dbExist = checkDatabase();
		if (dbExist)
		{
			openDatabase();
		}
		else
		{
			System.out.println("Database Doesn't exist");
			try{
				createDatabase();
			}
			catch (IOException ioe)
			{
			//System.out.println("Error creating database");
				ioe.printStackTrace();
			}
		}

	}
	
	public void createDatabase() throws IOException
	{
/*		File f = new File(DB_PATH);
*		if ( !(f.exists()) )
*			f.mkdir();
*/		
		boolean dbExists = checkDatabase();
		
		if (dbExists)
		{}  //It already exists, no need to do anything
		
		else
		{
			this.getReadableDatabase();
			try { copyDatabase(); }
			catch (IOException e)
			{
				throw new Error ("Error copying database.");
			}
		}
		
	}
	
	private boolean checkDatabase()
	{
		//SQLiteDatabase checkDB = null;
		boolean checkDB = false;
		try
		{
			String exDBPath = DB_PATH + DB_NAME;
			File dbFile = new File(exDBPath);
			//checkDB = SQLiteDatabase.openDatabase(exDBPath, null, SQLiteDatabase.OPEN_READONLY);
			checkDB = dbFile.exists();
		}
		
		catch (SQLiteException e)
		{
			//checkDB = null;
			//return false;
			System.out.println ("Database doesn't exist");
		}
		
		return checkDB;
		
		//if(checkDB != null)
		//{
		//	checkDB.close();
		//}
		
		// if the database does exist, return true, else false
		//return checkDB != null ? true : false;
		//return checkDB != null ? true : false;
	}
	
	// copy database from assets folder to system folder
	private void copyDatabase() throws IOException
	{
		AssetManager assMan = exContext.getAssets();
		InputStream input = assMan.open(DB_NAME);
		String dataFileName = DB_PATH + DB_NAME;
		OutputStream output = new FileOutputStream (dataFileName);
		
		byte[] buffer = new byte[1024];
		int length;
		while ((length = input.read(buffer)) > 0 )
		{
			output.write(buffer, 0, length);
		}
		
		output.flush();
		output.close();
		input.close();
	}
	
	// open the database
	public void openDatabase() throws SQLException
	{
		String dataFilePath = DB_PATH + DB_NAME;
		exDatabase = SQLiteDatabase.openDatabase(dataFilePath, null, SQLiteDatabase.OPEN_READONLY);
	}
	
	@Override
	public synchronized void close()
	{
		if (exDatabase 	!= null)
			exDatabase.close();
		super.close();
	}
	
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		
	}
	
	
}


